<?php
/*
* File home.php for project Rijlesplanner.nl
* Created on 19 nov 2007 at 21:55:11 by Ivar Pruijn
*/
class home extends modules {

	// Properties:
	protected $name = "home";
	protected $title = "Rijlesplanner - Goed geregeld!";
	protected $subtitle = "";

	// Methods:

	function process(& $template = null) {
		$this->template = $template;
		parent::loadModule("authentication",$template);
		parent::loadModule("tellafriend",$template);
		parent::loadModule("reviews",$template,"randomBerichten");
		parent::loadModule("ads",$template);
		parent::loadModule("statistics",$template);
		if (!empty ($_POST)) {
			if (!$this->auth->accessTo($this->name, null, $_POST['action'])) {
				$this->actionAccessDenied();
			} else {
				switch ($_POST['action']) {
					case "akkoordeigenverklaring" :
					if ($this->user instanceof Leerling) {
						$this->akkoordEigenVerklaring();
						$this->template->assign('moduleTemplate', 'modules/home/eigenverklaringakkoord_leerling.tpl');
					}
					break;
					case "akkoordalgemenevoorwaarden" :
					if ($this->user instanceof Leerling) {
						$this->akkoordAlgemeneVoorwaarden();
						$this->template->assign('moduleTemplate', 'modules/home/algemenevoorwaardenakkoord_leerling.tpl');
					}
					break;
				}
			}
		} else {
			if (!empty ($_GET['option'])) {
				if (!$this->auth->accessTo($this->name, $_GET['option'])) {
					$this->optionAccessDenied();
				} else {
					switch ($_GET['option']) {
						case "eigenverklaring" :
						if ($this->user instanceof Leerling) {
							$this->eigenVerklaring();
							$this->template->assign('moduleTemplate', 'modules/home/eigenverklaring_leerling.tpl');
						}
						break;
						case "algemenevoorwaarden" :
						if ($this->user instanceof Leerling) {
							$this->algemeneVoorwaarden();
							$this->template->assign('moduleTemplate', 'modules/home/algemenevoorwaarden_leerling.tpl');
						}
						break;
						case "algemenevoorwaardendownloaden" :
						if ($this->user instanceof Leerling) {
							$this->showDownloadAV();
						}
						break;
						default :
						if ($this->user instanceof Leerling) {
							$this->showMededelingen();
						}
						$this->template->assign('moduleTemplate', 'modules/home/home.tpl');
						break;
					}
				}
			} else {
				if ($this->user instanceof Leerling) {
					$this->showMededelingen();
				}
				$this->template->assign('moduleTemplate', 'modules/home/home.tpl');
			}
		}
	}

	function showMededelingen() {
		$errors = array ();
		try {
			$result = $this->wsclient->WS_Rijlesplanner_GetContent(array (
			'RijschoolId' => $this->user->getRijschoolId(),
			'SessionId' => $_SESSION['SessionId'],
			'PageId' => 'mededelingen'
			));
			$this->logger->log("Mededelingen result: ".print_r($result,true),PEAR_LOG_DEBUG);
			$content = $result->Content;
			$this->template->assign('mededelingen',$content);
		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_INFO);
			switch ($e->faultcode) {
				default :
				$errorcode = 64;
				$errormessage = 'Het ophalen van de persoonlijk pagina content is mislukt("'.$e->faultstring.'")';
				break;
			}
			$errors[] = array (
			'code' => $errorcode,
			'message' => $errormessage
			);
		}
	}
	
	function showDownloadAV() {
		$errors = array ();
		try {
			$result = $this->wsclient->WS_Rijlesplanner_GetFile(array (
			'RijschoolId' => $this->user->getRijschoolId(),
			'SessionId' => $_SESSION['SessionId'],
			'PageId' => 'av_file'
			));
			
			# Spuug alles uit en stop de executie van het script. (We laten dus geen templates zien)
			
			header("Pragma: ");
			header("Cache-Control: ");
			header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
			header("Last-Modified: " . gmdate("D, d M Y H:i ") . " GMT");
			header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1
			header('Content-Disposition: attachment; filename="'.$result->Filename.'"');
			header("Content-Type: application/download");
			header("Content-Description: File Transfer");     
			echo base64_decode($result->Content);
			exit;
		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_INFO);
			switch ($e->faultcode) {
				default :
				$errorcode = 64;
				$errormessage = 'Het ophalen van de persoonlijk pagina content is mislukt("'.$e->faultstring.'")';
				break;
			}
			$errors[] = array (
			'code' => $errorcode,
			'message' => $errormessage
			);
		}
	}
	
	function algemeneVoorwaarden() {
		$errors = array ();
		try {
			$result = $this->wsclient->WS_Rijlesplanner_GetContent(array (
			'RijschoolId' => $this->user->getRijschoolId(),
			'SessionId' => $_SESSION['SessionId'],
			'PageId' => 'algemenevoorwaarden'
			));
			$this->logger->log("Algemene voorwaarden result: ".print_r($result,true),PEAR_LOG_DEBUG);
			$content = $result->Content;
			$this->template->assign('pageContent', $content);
		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_INFO);
			switch ($e->faultcode) {
				default :
				$errorcode = 64;
				$errormessage = 'Het ophalen van de persoonlijk pagina content is mislukt("'.$e->faultstring.'")';
				break;
			}
			$errors[] = array (
			'code' => $errorcode,
			'message' => $errormessage
			);
			$this->setSubtitle($errormessage);
		}
		$this->template->assign('errors', $errors);
	}

	function akkoordAlgemeneVoorwaarden() {
		$akkoordResult = false;
		$akkoord = false;
		$this->errors = array ();
		try {
			if (isset ($_POST['nietakkoord']) && !empty($_POST['nietakkoord'])) {
				$akkoord = false;
			} else if (isset ($_POST['akkoord']) && !empty($_POST['akkoord'])) {
				$akkoord = true;
			}
			$this->logger->log("Akkoord gaan: $akkoord");
			$result = $this->wsclient->WS_Rijlesplanner_Akkoord_Algemene_Voorwaarden(array (
			'RijschoolId' => $this->user->getRijschoolId(),
			'SessionId' => $_SESSION['SessionId'],
			'Akkoord' => $akkoord,
			));
			$this->logger->log(print_r($result,true),PEAR_LOG_DEBUG);
			$akkoordResult = $result->Success;

			if ($akkoordResult && $akkoord) {
				$_SESSION['NietAkkoord'] = array_diff($_SESSION['NietAkkoord'], array("algemene_voorwaarden_verplicht"));
				header("Location: /");
			}

		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_INFO);
			switch ($e->faultcode) {
				case 1 :
				$errorcode = 69;
				$errormessage = 'Dit type voorwaarden is onbekend ("' . $e->faultstring . '")';
				break;
				default :
				$errorcode = 68;
				$errormessage = 'Er is een onbekende fout opgetreden bij het akkoord van de Algemene Voorwaarden ("' . $e->faultstring . '")';
				break;
			}
			$this->errors[] = array (
			'code' => $errorcode,
			'message' => $errormessage
			);
			$this->setSubtitle($errormessage);
		}
		$this->template->assign('akkoordResult', $akkoordResult);
		$this->template->assign('akkoord', $akkoord);
		$this->template->assign('errors', $this->errors);
		return $akkoordResult;
	}

	function eigenVerklaring() {
		$errors = array ();
		try {
			$result = $this->wsclient->WS_Rijlesplanner_GetContent(array (
			'RijschoolId' => $this->user->getRijschoolId(),
			'SessionId' => $_SESSION['SessionId'],
			'PageId' => 'eigenverklaring'
			));
			$this->logger->log("Eigen verklaring: ".print_r($result,true),PEAR_LOG_DEBUG);
			$content = $result->Content;
			$this->template->assign('pageContent', $content);
		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_INFO);
			switch ($e->faultcode) {
				default :
				$errorcode = 64;
				$errormessage = 'Het ophalen van de persoonlijk pagina content is mislukt("'.$e->faultstring.'")';
				break;
			}
			$errors[] = array (
			'code' => $errorcode,
			'message' => $errormessage
			);
			$this->setSubtitle($errormessage);
		}
		$this->template->assign('errors', $errors);
	}

	function akkoordEigenVerklaring() {
		$akkoordResult = false;
		$aantalJa = 0;
		$akkoord = false;
		$antwoorden = array();
		$this->errors = array ();
		try {
			if (isset ($_POST['nietakkoord']) && !empty($_POST['nietakkoord'])) {
				$akkoord = false;
			} else {
				$akkoord = true;
			}

			if ($_POST['q1'] > 0) {
				$antwoorden[] = '1';
			}
			if ($_POST['q2'] > 0) {
				$antwoorden[] = '2';
			}
			if ($_POST['q3'] > 0) {
				$antwoorden[] = '3';
			}
			if ($_POST['q4'] > 0) {
				$antwoorden[] = '4';
			}
			if ($_POST['q5'] > 0) {
				$antwoorden[] = '5';
			}
			if ($_POST['q6'] > 0) {
				$antwoorden[] = '6';
			}
			if ($_POST['q7'] > 0) {
				$antwoorden[] = '7';
			}
			if ($_POST['q8a'] > 0) {
				$antwoorden[] = '8a';
			}
			if ($_POST['q8b'] > 0) {
				$antwoorden[] = '8b';
			}
			if ($_POST['q9'] > 0) {
				$antwoorden[] = '9';
			}
			if ($_POST['q10'] > 0) {
				$antwoorden[] = '10';
			}

			$result = $this->wsclient->WS_Rijlesplanner_Akkoord_Eigen_Verklaring(array (
			'RijschoolId' => $this->user->getRijschoolId(),
			'SessionId' => $_SESSION['SessionId'],
			'Akkoord' => $akkoord,
			'Antwoorden' => serialize($antwoorden)
			));
			$this->logger->log(print_r($result,true),PEAR_LOG_DEBUG);
			$akkoordResult = $result->Success;
			$aantalJa = $result->AantalJa;

			# Niet akkoord -> Dashboard melding teruggeven
			if(!$akkoordResult) {
				$result = $this->wsclient->WS_Rijlesplanner_GetContent(array (
				'RijschoolId' => $this->user->getRijschoolId(),
				'SessionId' => $_SESSION['SessionId'],
				'PageId' => 'eigenverklaring_afwijzing'
				));
				$this->template->assign('eigenverklaring_afwijzing_tekst',$result->Content);
			} else {
				$_SESSION['NietAkkoord'] = array_diff($_SESSION['NietAkkoord'], array("eigen_verklaring"));
			}

		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_INFO);
			switch ($e->faultcode) {
				default :
				$errorcode = 67;
				$errormessage = 'Er is een onbekende fout opgetreden bij het akkoord gaan met de Eigen Verklaring ("' . $e->faultstring . '")';
				break;
			}
			$this->errors[] = array (
			'code' => $errorcode,
			'message' => $errormessage
			);
			$this->setSubtitle($errormessage);
		} catch (AkkoordEigenVerklaringException $e) {
			$this->errors[] = array (
			'code' => $e->getCode(),
			'message' => $e->getMessage()
			);
			$this->setSubtitle($e->getMessage());
		}
		$this->template->assign('akkoordResult', $akkoordResult);
		$this->template->assign('aantalJa', $aantalJa);
		$this->template->assign('errors', $this->errors);
		return $akkoordResult;
	}

	public function addError($message, $code) {
		$this->errors[] = array (
		'code' => $code,
		'message' => $message
		);
		$this->setSubtitle($message);
	}

	/**
	* void __construct ()
	* This is the constructor method for the Modules class.
	*/
	public function __construct() {
		parent::__construct();
	}

	public function getTitle() {
		return $this->title;
	}

	public function getSubTitle() {
		if(!empty($_GET['option'])) {
			$subtitle = "";
			if (isset($this->moduleOptions)) {
				foreach($this->moduleOptions AS $mOpt) {
					if($mOpt['name'] == $_GET['option']) {
						$subtitle = $mOpt['title'];
					}
				}
			}
			return $subtitle;
		} else {
			return $this->subtitle;
		}
	}

	public function __toString() {
		return $this->title;
	}
}
?>